<?php 
/**
* 
*/
class Db
{
	private $host;
	private $user;
	private $userPwd;
	private $dbh;
	
	function __construct($host,$user,$userpwd)
	{
		$this->host =$host ;
		$this->user=$user;
		$this->userpwd= $userpwd;
		$this->dbh  = new PDO("mysql:host=$host;dbname=1704phpA",$user,$userPwd);
	}
	/*
	定义插入的方法首先传入表名以及要插入的数组
	 */
	public function insertData($table,$array)
	{
		// 定义两个变量
		$filed = '';
        $val = '';
        // 循环数组
		foreach ($array as $key => $value) {
           $filed .= ",'$value'";
           $val .=",`$key`";
           // var_dump($val);die;
		}
		// 截取多余的逗号
		$filed = substr($filed,1);
		$val = substr($val,1);
		// 插入到表数据里边
		$sql="insert into $table($val)  values($filed)";
        
		$data= $this->dbh->exec($sql);
        // 返回数据
		return $data;
	}
    /*
    定义一个删除的方法，传入参数 
     */
	public function delData($table,$id)
	{
		// 定义一个查询的语句
		$sql = "delete from `$table` where id = '$id'";
		// var_dump($sql);
       // 返回数据
		return $this->dbh->exec($sql);
	}
	// 查询一条记录的方法
	public function getOne($table)
	{
		
		$sql = "select * from $table";

		$data = $this->dbh->query($sql);

		return $data->fetch(2);
	}
	public function getAll($table)
	{
		$sql = "select * from $table";

		$data = $this->dbh->query($sql);

		return $data->fetchAll(2);
	}
	public function upData($table,$array,$id)
	{
		$sql="update $table set name='$array' where id= $id";
		return $this->dbh->exec($sql);
	}
}
$dbh = new Db('127.0.0.1','root','root');
// $array = array('account'=>'12453@qq.com','name'=>'白换欣','desc'=>'是我何贱人看了父母官');
// var_dump($dbh->insertData('user',$array));
// var_dump($dbh->delData('user',1));'
// var_dump($dbh->getOne('user'));
// var_dump($dbh->getAll('user'));
// var_dump($dbh->upData('user','部门会议',2));
 ?>